{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Gmailを取得する"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 環境構築\n",
    "\n",
    "* Rubyは2.3系"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Gemfile\n",
    "\n",
    "```\n",
    "$ vi Gemfile\n",
    "================================================\n",
    "# frozen_string_literal: true\n",
    "\n",
    "source \"https://rubygems.org\"\n",
    "\n",
    "git_source(:github) {|repo_name| \"https://github.com/#{repo_name}\" }\n",
    "\n",
    "gem \"google-api-client\"\n",
    "gem 'debase', '0.2.2.beta8'\n",
    "gem 'ruby-debug-ide', '0.6.1.beta9'\n",
    "================================================\n",
    "\n",
    "$ bundle install --path vendor/bundle\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### アカウント情報準備\n",
    "\n",
    "* https://developers.google.com/gmail/api/quickstart/ruby\n",
    "    * jsonファイルをダウンロードしてclient_secret.jsonにする\n",
    "    * 「OAuth 2.0 クライアント ID」からクライアントIDとクライアントシークレットをコピペで控える\n",
    "    * quickstart.rbをコピペで準備\n",
    "\n",
    "<img src=\"img/secret.png\" alt=\"情報\" title=\"情報\" width=\"600\" height=\"300\" align=\"left\" />  \n",
    "\n",
    "<br clear=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### googleauth修正(非推奨)\n",
    "\n",
    "* そのままでは使えなかったので情報をコードに直接埋め込んだ\n",
    "\n",
    "```\n",
    "$ vi vendor/bundle/ruby/2.3.0/gems/googleauth-0.6.2/lib/googleauth/client_id.rb\n",
    "================================================\n",
    "■ before\n",
    "      def self.from_hash(config)\n",
    "        raise 'Hash can not be nil.' if config.nil?\n",
    "        raw_detail = config[INSTALLED_APP] || config[WEB_APP]\n",
    "        raise MISSING_TOP_LEVEL_ELEMENT_ERROR if raw_detail.nil?\n",
    "        ClientId.new(raw_detail[CLIENT_ID], raw_detail[CLIENT_SECRET])\n",
    "      end\n",
    "\n",
    "■ after\n",
    "      def self.from_hash(config)\n",
    "        raise 'Hash can not be nil.' if config.nil?\n",
    "        #raw_detail = config[INSTALLED_APP] || config[WEB_APP]\n",
    "        #raise MISSING_TOP_LEVEL_ELEMENT_ERROR if raw_detail.nil?\n",
    "        #ClientId.new(raw_detail[CLIENT_ID], raw_detail[CLIENT_SECRET])\n",
    "        ClientId.new(\"123456789123-abc1d2efghif3klm456no78pq9rstuv0.apps.googleusercontent.com\", \"ABCDEfgHIFk1lmNOP2qRstu3\")\n",
    "      end\n",
    "================================================\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 参考\n",
    "    * [rubyでの正しいgmailの取得方法を教える](https://qiita.com/hatappi/items/8d701c934b143e490599)\n",
    "\n",
    "例quickstart.rbに書き込む\n",
    "\n",
    "```\n",
    "# How to use Gmail API\n",
    "# https://qiita.com/hatappi/items/8d701c934b143e490599\n",
    "# Class: Google::Apis::GmailV1::GmailService\n",
    "# http://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/GmailV1/GmailService\n",
    "\n",
    "### get message id\n",
    "user_id = 'me'\n",
    "# result = service.list_user_messages(user_id)\n",
    "result = service.list_user_messages(user_id, q: \"from:info@hoge.example.co.jp\")\n",
    "message_ids = result.messages.map { |message| message.id }\n",
    "\n",
    "### get messages\n",
    "messages = []\n",
    "message_ids.each do |id|\n",
    "  messages << service.get_user_message(user_id, id)\n",
    "end\n",
    "\n",
    "puts messages[4].snippet\n",
    "```\n",
    "\n",
    "正規表現練習\n",
    "\n",
    "```\n",
    "0> messages[4].payload.body.data.match(%r{(\"http)://pmrd(.*?\")})[0]\n",
    "=> \"\\\"http://hoge.example.co.jp/?r=AbC1DeF2gHI%3J&k=LMN4OP5&u=6QRST7UV\\\"\"\n",
    "```\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.3",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
